package com.gpnu7_412.yanzhi.exception;

import com.gpnu7_412.yanzhi.exception.custom.*;
import com.gpnu7_412.yanzhi.status.Impl.ResultCode;
import com.gpnu7_412.yanzhi.util.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 * @author ZZH
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {
    @ExceptionHandler(value = DataNotFoundException.class)
    public Result dataNotFoundException(DataNotFoundException e) {
        return new Result(ResultCode.DATA_NOT_FOUNT_EXCEPTION, e.getMessage());
    }

    @ExceptionHandler(value = DataInconsistencyException.class)
    public Result dataInconsistencyException(DataInconsistencyException e) {
        return new Result(ResultCode.DATA_INCONSISTENCY_EXCEPTION, e.getMessage());
    }

    @ExceptionHandler(value = SQLExecuteException.class)
    public Result sqlExecuteException(SQLExecuteException e) {
        return new Result(ResultCode.SQL_EXECUTE_EXCEPTION, e.getMessage());
    }

    @ExceptionHandler(value = RequestDataException.class)
    public Result sqlExecuteException(RequestDataException e) {
        return new Result(ResultCode.REQUEST_DATA_EXCEPTION, e.getMessage());
    }

    @ExceptionHandler(value = PermissionDeniedException.class)
    public Result sqlExecuteException(PermissionDeniedException e) {
        return new Result(ResultCode.PERMISSION_DENIED_EXCEPTION, e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public Result exception(Exception e) {
        e.printStackTrace();
        return new Result(ResultCode.FAILED, "服务器错误！", e.getMessage());
    }
}
